﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows;

namespace Base64
{
    /// <summary>
    /// GUI for encoding to base64 and decoding from base64. 
    /// For the decoding, the base64 string could be read from a text file or enter into the text box.
    /// </summary>
    /// <datetime>04/10/2012-11:08</datetime>
    public partial class Form_Base64EncodeDecode : Form
    {
        public Form_Base64EncodeDecode()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Handles the Load event of the Form1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        /// <datetime>04/10/2012-11:14</datetime>
        private void Form_Base64EncodeDecode_Load(object sender, EventArgs e)
        {          
            btn_encode.Enabled = false;
            btn_saveEncode.Enabled = false;
            btn_loadBase64Text.Enabled = false;
            btn_saveToDecodedFile.Enabled = false;
        }

        /*************** Encoding GUI **********************/

        /// <summary>
        /// Encode the selected binary file to base64.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        /// <datetime>04/10/2012-11:20</datetime>
        private void btn_encode_Click(object sender, EventArgs e)
        {
            if (textBox_FileToEncode.Text != "")
            {
                string FileToEncode = textBox_FileToEncode.Text;
                string EncodedData = Encode64.EncodeFileToBase64(FileToEncode);
                textBox_encodedText.Text = EncodedData;
            }
        }

        /// <summary>
        /// Set the status of the encoded button base on the content of the text box which contain the file to encode.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        /// <datetime>04/10/2012-11:22</datetime>
        private void textBox_FileToEncode_TextChanged(object sender, EventArgs e)
        {
            if (textBox_FileToEncode.Text != "")
            {             
                btn_encode.Enabled = true;
            }              
            else
                btn_encode.Enabled = false;
        }


        /// <summary>
        /// Save the base64 encoded string to file.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        /// <datetime>04/10/2012-11:23</datetime>
        private void btn_saveEncode_Click(object sender, EventArgs e)
        {
            string EncodedData;
            string FileToWriteEncoded;

            if (textBox_FileToSaveEncoded.Text != "" && textBox_encodedText.Text != "")
            {
                EncodedData = textBox_encodedText.Text;
                FileToWriteEncoded = textBox_FileToSaveEncoded.Text;
                Encode64.WriteEncodedToFile(EncodedData, FileToWriteEncoded);

                btn_saveEncode.Text = "Done";
                btn_saveEncode.Enabled = false;
            }         
        }

        /// <summary>
        /// The file path and file name to save the base64 string.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        /// <datetime>04/10/2012-11:24</datetime>
        private void textBox_FileToSaveEncoded_TextChanged(object sender, EventArgs e)
        {
            if (textBox_FileToSaveEncoded.Text != "" && textBox_encodedText.Text != "")
            {
                btn_saveEncode.Enabled = true;
                btn_saveEncode.Text = "Save";
            }                
            else
                btn_saveEncode.Enabled = false;
        }

        /// <summary>
        /// Browse to the disired binary file to encode to base64.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        /// <datetime>04/10/2012-11:25</datetime>
        private void btn_browseFileToEncode_Click(object sender, EventArgs e)
        {
            // Create an instance of the open file dialog box.
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            // Set filter options and filter index.
            openFileDialog1.Filter = "PDF|*.pdf|Text Files|*.txt|All Files|*.*";
            openFileDialog1.FilterIndex = 1;

            openFileDialog1.Multiselect = false;

            DialogResult userClickedOK = openFileDialog1.ShowDialog();

            if (userClickedOK == DialogResult.OK)
            {
                textBox_FileToEncode.Text = openFileDialog1.FileName;
            }
        }

        /// <summary>
        /// Browse to the file directory to save the base64 encoded string.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        /// <datetime>04/10/2012-11:26</datetime>
        private void button_browseFileToSaveEncode_Click(object sender, EventArgs e)
        {
           SaveFileDialog saveFileDialog1 = new SaveFileDialog();
           saveFileDialog1.Filter = "Text Files|*.txt|All Files|*.*";
           saveFileDialog1.Title = "Save Encoded Text to File";
           saveFileDialog1.ShowDialog();

           // If the file name is not an empty string open it for saving.
           if(saveFileDialog1.FileName != "")
           {
               textBox_FileToSaveEncoded.Text = saveFileDialog1.FileName;

               string EncodedData;
               string FileToWriteEncoded;

               if (textBox_FileToSaveEncoded.Text != null && textBox_encodedText.Text != null)
               {
                   EncodedData = textBox_encodedText.Text;
                   FileToWriteEncoded = textBox_FileToSaveEncoded.Text;
                   Encode64.WriteEncodedToFile(EncodedData, FileToWriteEncoded);
               }
           }
        }

        /// <summary>
        /// Set the status of the save button depending on the content of the text box which contains the base64 string.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        /// <datetime>04/10/2012-11:28</datetime>
        private void textBox_encodedText_TextChanged(object sender, EventArgs e)
        {
            if (textBox_FileToSaveEncoded.Text != "" && textBox_encodedText.Text != "")
            {
                btn_saveEncode.Enabled = true;
                btn_saveEncode.Text = "Save";
            }
            else
                btn_saveEncode.Enabled = false;
        }


        /*************** Decoding GUI **********************/

        /// <summary>
        /// Set the status of the load button, depending on the content of the text box which contains the file path of base64 text file.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        /// <datetime>04/10/2012-11:32</datetime>
        private void textBox_FileContainingBase64_TextChanged(object sender, EventArgs e)
        {
            if (textBox_FileContainingBase64.Text != "")
            {
                btn_loadBase64Text.Enabled = true;
            }
            else
                btn_loadBase64Text.Enabled = false;
        }

        /// <summary>
        /// Select the file which contains the base64 text to be decoded.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        /// <datetime>04/10/2012-11:36</datetime>
        private void btn_browseFileToDecode_Click(object sender, EventArgs e)
        {
            // Create an instance of the open file dialog box.
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            // Set filter options and filter index.
            openFileDialog1.Filter = "Text Files|*.txt|All Files|*.*";
            openFileDialog1.FilterIndex = 1;

            openFileDialog1.Multiselect = false;

            DialogResult userClickedOK = openFileDialog1.ShowDialog();

            if (userClickedOK == DialogResult.OK)
            {
                textBox_FileContainingBase64.Text = openFileDialog1.FileName;
            }
        }

        /// <summary>
        /// Read the base64 text file and display in the text box.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        /// <datetime>04/10/2012-11:40</datetime>
        private void btn_loadBase64Text_Click(object sender, EventArgs e)
        {
            if (textBox_FileContainingBase64.Text != "")
            {
                string EncodedDataRead = Decode64.readEncodeDataFromFile(textBox_FileContainingBase64.Text);
                textBox_base64Text.Text = EncodedDataRead;
            }  
        }

        /// <summary>
        /// Set the status of the save button depending on the contents of the text box which contains the base64 string.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        /// <datetime>04/10/2012-11:41</datetime>
        private void textBox_base64Text_TextChanged(object sender, EventArgs e)
        {
            if (textBox_DecodedFile.Text != "" && textBox_base64Text.Text != "")
            {
                btn_saveToDecodedFile.Enabled = true;
                btn_saveToDecodedFile.Text = "Save";
            }
            else
                btn_saveToDecodedFile.Enabled = false;
        }

        /// <summary>
        /// Set the status of the save button depending on the contents of the text box which contains the file path to save the decoded binary data.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        /// <datetime>04/10/2012-11:42</datetime>
        private void textBox_DecodedFile_TextChanged(object sender, EventArgs e)
        {
            if (textBox_DecodedFile.Text != "" && textBox_base64Text.Text != "")
            {
                btn_saveToDecodedFile.Enabled = true;
                btn_saveToDecodedFile.Text = "Save";
            }
            else
                btn_saveToDecodedFile.Enabled = false;
        }

        /// <summary>
        /// Save the decoded binary data to a file.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        /// <datetime>04/10/2012-11:44</datetime>
        private void btn_saveToDecodedFile_Click(object sender, EventArgs e)
        {
            string EncodedData;
            string DecodedOutputFile;

            if (textBox_DecodedFile.Text != "" && textBox_base64Text.Text != "")
            {
                EncodedData = textBox_base64Text.Text;
                DecodedOutputFile = textBox_DecodedFile.Text;
                Decode64.DecodeBase64(EncodedData, DecodedOutputFile);

                btn_saveToDecodedFile.Text = "Done";
                btn_saveToDecodedFile.Enabled = false;
            }          
        }

        /// <summary>
        /// Browse to the directory and enter the file name to save the resulting from the base64 decoding.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        /// <datetime>04/10/2012-11:39</datetime>
        private void btn_browseDecodedFile_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Filter = "PDF|*.pdf|Text Files|*.txt|All Files|*.*";
            saveFileDialog1.Title = "Save Decode to Disired File";
            saveFileDialog1.ShowDialog();

            // If the file name is not an empty string open it for saving.
            if (saveFileDialog1.FileName != "")
            {
                textBox_DecodedFile.Text = saveFileDialog1.FileName;
                //btn_saveToDecodedFile.PerformClick();
            }
        }

        /// <summary>
        /// Exit the application.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        /// <datetime>04/10/2012-11:51</datetime>
        private void btn_exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
